//
//  XJRegistView.m
//  XinJiangMall
//
//  Created by Tzyang on 2018/4/8.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJRegistView.h"

#import "AttributedLabel.h"

#import "XJCodeView.h"

#import "XJNet.h"
#import "XJRequestHelper.h"

@interface XJRegistView ()<UITextFieldDelegate,XJCodeDelegate>

@property (nonatomic, strong) UIButton * nextButton;//下一步
@property (nonatomic, strong) AttributedLabel * registProtocolLab;//注册协议
@property (nonatomic ,strong) XJCodeView * xjCodeView;

@end

@implementation XJRegistView

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = WHITE_COLOR;
        [self setupSubviews];
    }
    return self;
}

- (ZYTextField *)registPhoneTextField{
    
    if (!_registPhoneTextField) {
        ZYTextField * registPhoneTextField = [[ZYTextField alloc]initWithFrame:CGRectMake(ADAPTWidth(22), ADAPTHeight(30), KWidth - ADAPTWidth(22)*2, ADAPTHeight(50)) placeholder:@"请输入手机号" keyboardType:UIKeyboardTypePhonePad leftViewImageName:@"login_phone" textEntry:NO];
        self.registPhoneTextField = registPhoneTextField;
        [self addSubview:registPhoneTextField];
    }
    return _registPhoneTextField;
}

- (ZYTextField *)registCodeTextField{
    
    if (!_registCodeTextField) {
        ZYTextField * registCodeTextField = [[ZYTextField alloc]initWithFrame:CGRectMake(ADAPTWidth(22), CGRectGetMaxY(self.registPhoneTextField.frame)+0.5, KWidth - ADAPTWidth(22)*2, ADAPTHeight(50)) placeholder:@"请输入验证码" keyboardType:UIKeyboardTypeNumberPad leftViewImageName:@"login_verify" textEntry:NO];
        self.registCodeTextField = registCodeTextField;
        [self addSubview:registCodeTextField];
    }
    return _registCodeTextField;
}

- (AttributedLabel *)registProtocolLab{
    
    if (!_registProtocolLab) {
        AttributedLabel * registProtocolLab = [AttributedLabel new];
        self.registProtocolLab = registProtocolLab;
        [self addSubview:registProtocolLab];
    }
    return _registProtocolLab;
    
}

- (XJCodeView *)xjCodeView{
    
    if (!_xjCodeView) {
        XJCodeView * xjCodeView = [[XJCodeView alloc]initWithTime:60 frame:CGRectMake(0, 0, 80, 30)];
        self.xjCodeView = xjCodeView;
    }
    return _xjCodeView;
    
}

- (UIButton *)nextButton{
    
    if (!_nextButton) {
        UIButton * nextButton = [UIButton new];
        self.nextButton = nextButton;
        [self addSubview:nextButton];
    }
    return _nextButton;
}

- (void)setupSubviews{
    
    //手机号
    self.registPhoneTextField.delegate = self;
    [self.registPhoneTextField addTarget:self action:@selector(regist_phoneTextDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.registPhoneTextField addTarget:self action:@selector(regist_phoneTextDidEnd:) forControlEvents:UIControlEventEditingDidEnd];
    
    //验证码
    self.xjCodeView.delegate = self;
    self.xjCodeView.codeBtn.backgroundColor = MAINS_COLOR;
    self.xjCodeView.codeBtn.userInteractionEnabled = NO;
    
    self.registCodeTextField.rightView = self.xjCodeView;
    self.registCodeTextField.rightViewMode = UITextFieldViewModeAlways;
    
    //注册协议
    self.registProtocolLab.text = @"点击下一步代表您已同意《新疆商城用户协议》";
    self.registProtocolLab.font = XJFONTSET_R(11);
    self.registProtocolLab.textColor = XJCOLOR_RGB(157, 165, 168);

    CGFloat registProtocolLabW = [UILabel labelWithLabel:self.registProtocolLab getWidthWithTitle:self.registProtocolLab.text font:self.registProtocolLab.font];
    CGFloat registProtocolLabH = [UILabel labelWithLabel:self.registProtocolLab getHeightByWidth:registProtocolLabW title:self.registProtocolLab.text font:self.registProtocolLab.font];
    self.registProtocolLab.sd_layout
    .topSpaceToView(self.registCodeTextField, ADAPTHeight(29))
    .centerXEqualToView(self)
    .widthIs(registProtocolLabW)
    .heightIs(registProtocolLabH);
    //这个地方有问题需要先设置 color
    self.registProtocolLab.attributedColor = XJCOLOR_RGB(13, 98, 154);
    self.registProtocolLab.attributedRange = NSMakeRange(11, 10);
    
    //下一步
    self.nextButton.backgroundColor = MAINS_COLOR;
    self.nextButton.userInteractionEnabled = NO; // 默认不可点,验证码发送成功修改状态。
    [self.nextButton setTitle:@"下一步" forState:UIControlStateNormal];
    [self.nextButton setTitleColor:XJCOLOR_RGB(17, 17, 17) forState:UIControlStateNormal];
    self.nextButton.titleLabel.font = XJFONTSET_R(15);
    self.nextButton.layer.masksToBounds = YES;
    self.nextButton.layer.cornerRadius = ADAPTHeight(40)/2;
    [self.nextButton addTarget:self action:@selector(nextBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.nextButton.sd_layout
    .leftSpaceToView(self, ADAPTWidth(25))
    .rightSpaceToView(self, ADAPTWidth(25))
    .topSpaceToView(self.registProtocolLab, ADAPTHeight(30))
    .heightIs(ADAPTHeight(40));
}

#pragma mark 手机号监听

- (void)regist_phoneTextDidChange:(UITextField *)textField{
    if ([textField.text isPhoneNumber]) {
        XJLog(@"手机号 = %@",textField.text);
        self.xjCodeView.codeBtn.backgroundColor = MAIN_COLOR;
        self.xjCodeView.codeBtn.userInteractionEnabled = YES;
    }else{
        self.xjCodeView.codeBtn.backgroundColor = MAINS_COLOR;
        self.xjCodeView.codeBtn.userInteractionEnabled = NO;
    }
    
}

- (void)regist_phoneTextDidEnd:(UITextField *)textField{
    
}

#pragma mark 验证码监听

#pragma mark 下一步
- (void)nextBtnClick{
    
    if (_delegate && [_delegate respondsToSelector:@selector(nextStepAction)]) {
        [_delegate nextStepAction];
    }
}



#pragma mark 获得验证码
- (void)getCheckCode:(NSString *)checkCode{
    
    XJWeakSelf(self);
//
//    [[XJRequestHelper shareHelper] setBlockWithReturnBlock:^(id returnValue) {
//
//    } failureBlock:^{
//
//    } errorBlock:^(id errorCode) {
//
//    }];
    
    
    [[XJNet loadNet] netWorkReachabilityState:^(BOOL netState) {
        if (netState) {
            
            [weakself requestCodeWithPhone:weakself.registPhoneTextField.text];
            
//            weakself.registPhoneTextField.text;
            
//            [[XJRequestHelper shareHelper] getCodeWithPhoneNumber:weakself.registPhoneTextField.text type:@"regist"];
        }else{
            XJLog(@"断开网络连接");
            [MBProgressHUD showError:@"断开网络连接"];
        }
    }];
    
    XJLog(@"验证码 :%@",checkCode);
}
- (void)requestCodeWithPhone:(NSString *)phone {
    [XJHttpTool requestSendCodeWithPhone:phone type:@"regist" successH:^(id responseObjc) {
        XJLog(@"成功:%@",responseObjc);
        // 修改该下一步按钮的点击状态
        self.nextButton.userInteractionEnabled = YES;
        self.nextButton.backgroundColor = MAIN_COLOR;
        [JRToast showWithText:@"验证码已发送"];

    } failureH:^(NSError *error) {
        XJLog(@"失败:%@",error);
        
        self.nextButton.userInteractionEnabled = NO;
        self.nextButton.backgroundColor = MAINS_COLOR;
        
        [MBProgressHUD showError:[NSString stringWithFormat:@"%@",[error localizedDescription]]];

    }];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    [self.registPhoneTextField endEditing:YES];
    
    [self.registCodeTextField endEditing:YES];

}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
